#!/usr/bin/make -f
# debian/rules for Free Pascal

DEB_DH_BUILDDEB_ARGS := -- -Z bzip2

# Documentation type to use pdf/html
ifeq (${DEBDOCTYPE},)
DOCTYPE=html
# HTML Converter l2h/4ht/hevea or html for (default)
CONVERTER=hevea
else
DOCTYPE=${DEBDOCTYPE}
CONVERTER=${DOCTYPE}
endif

# Detect name of new compiler, take care that debian uses amd64 instead of x86_64
CPU_SOURCE=$(subst amd64,x86_64,$(shell dpkg-architecture -qDEB_BUILD_ARCH))
CPU_TARGET=$(subst amd64,x86_64,$(shell dpkg-architecture -qDEB_HOST_ARCH))

ifeq ($(CPU_TARGET),m68k)
PPSUF=68k
endif
ifeq ($(CPU_TARGET),i386)
PPSUF=386
endif
ifeq ($(CPU_TARGET),x86_64)
PPSUF=x64
endif
ifeq ($(CPU_TARGET),powerpc)
PPSUF=ppc
endif
ifeq ($(CPU_TARGET),alpha)
PPSUF=axp
endif
ifeq ($(CPU_TARGET),arm)
PPSUF=arm
endif
ifeq ($(CPU_TARGET),sparc)
PPSUF=sparc
endif

ifneq ($(CPU_SOURCE),$(CPU_TARGET))
PPPRE=ppcross
else
PPPRE=ppc
endif

PPNEW=$(PPPRE)$(PPSUF)

# Reset FPC and FPCDIR if it was set
FPC=
FPCDIR=
# Get Package version and FPC version out of changelog file
FPCVERSION:=$(shell dpkg-parsechangelog | sed -ne's,^Version: \([0-9.]*\).*,\1,p')
FPCTARGET=$(CPU_TARGET)-linux
# Get directories
CURDIR:=$(shell pwd)
BUILD_DIR=$(CURDIR)/debian/build
INSTALL_DIR=$(CURDIR)/debian/tmp
DOC_DIR=$(INSTALL_DIR)/usr/share/doc
EXAMPLE_TEMP=$(DOC_DIR)/fpc-$(FPCVERSION)
SRC_DIR=$(INSTALL_DIR)/usr/share/fpcsrc/${FPCVERSION}
# Get utils
NEWPP=$(CURDIR)/fpcsrc/compiler/$(PPNEW)
NEWFPDOC=$(CURDIR)/fpcsrc/utils/fpdoc/fpdoc
NEWFPCMAKE=$(CURDIR)/fpcsrc/utils/fpcm/fpcmake
# Create default options
BUILDOPTS=PP=$(NEWPP)
INSTALLOPTS=PP=$(NEWPP) FPCMAKE=$(NEWFPCMAKE) INSTALL_PREFIX=$(INSTALL_DIR)/usr

export GDBLIBDIR := /usr/lib

# export DH_VERBOSE=1

###################
# Clean
#

CLEAN_ALL = (find -name '*.a' -or -name '*.o' -or -name '*.ppu' | xargs rm)
clean: clean-patched unpatch
clean-patched:
	@echo "--- Cleaning"
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp install-arch-stamp arrange-arch-stamp configure-stamp
	rm -f build-doc-stamp install-doc-stamp
	rm -f build-indep-stamp install-indep-stamp
	rm -f debian-files-stamp
	rm -f debian/*.files debian/*.docs debian/*.examples debian/*.postinst debian/*.postrm
	rm -f debian/fpc-depends.1
	$(MAKE) -C fpcsrc compiler_distclean
	$(MAKE) -C fpcsrc rtl_distclean
	$(MAKE) -C fpcsrc packages_distclean
	$(MAKE) -C fpcsrc fv_distclean
	$(MAKE) -C fpcsrc ide_distclean
	$(MAKE) -C fpcsrc utils_distclean
	$(MAKE) -C fpcdocs clean
	-${CLEAN_ALL}
	dh_clean

patch: patch-stamp

patch-stamp:
	dpatch apply-all
	touch patch-stamp

unpatch:
	dpatch deapply-all
	rm -rf patch-stamp debian/patched

###################
# Debian files
#

debian-files: debian-files-stamp
debian-files-stamp: patch
	@echo "--- Creating/fixing *.install files"
	/bin/bash debian/fixdeb debian $(FPCTARGET) $(PPNEW)
	touch debian-files-stamp

###################
# Arch packages
#

configure: configure-stamp
configure-stamp:
	dh_testdir
	# Remove auto-generated make files.
	find -name Makefile.fpc -execdir 'sh' '-c' '${RM} $$(basename {} .fpc)' ';'
	# Regenrate make files using fpcmake.
	fpcmake -r -Tall */Makefile.fpc
	touch configure-stamp

build-arch: build-arch-stamp
build-arch-stamp: debian-files-stamp configure-stamp
	@echo "--- Building"
	dh_testdir
# First make a new Compiler and RTL using a make cycle
	$(MAKE) -C fpcsrc compiler_cycle
	$(MAKE) -C fpcsrc rtl_clean rtl_smart $(BUILDOPTS)
	$(MAKE) -C fpcsrc packages_base_smart $(BUILDOPTS)
	$(MAKE) -C fpcsrc packages_fcl_smart $(BUILDOPTS)
	$(MAKE) -C fpcsrc fv_smart $(BUILDOPTS)
	$(MAKE) -C fpcsrc packages_extra_smart $(BUILDOPTS)
	$(MAKE) -C fpcsrc ide_all $(BUILDOPTS)
	$(MAKE) -C fpcsrc utils_all $(BUILDOPTS)
	touch build-arch-stamp

install-arch: install-arch-stamp
install-arch-stamp: build-arch-stamp debian-files-stamp
	@echo "--- Installing"
	dh_testdir
	dh_testroot
	dh_clean -k
# Specify the compiler to use so installing will use the correct versioned dir
	$(MAKE) -C fpcsrc compiler_distinstall $(INSTALLOPTS)
	$(MAKE) -C fpcsrc rtl_distinstall $(INSTALLOPTS)
	$(MAKE) -C fpcsrc fv_distinstall $(INSTALLOPTS)
	$(MAKE) -C fpcsrc packages_distinstall $(INSTALLOPTS)
	$(MAKE) -C fpcsrc ide_distinstall $(INSTALLOPTS) INSTALL_DOCDIR=$(DOC_DIR)/fp-ide
	$(MAKE) -C fpcsrc utils_distinstall $(INSTALLOPTS)
# Copy examples to the correct doc dir
	/bin/bash debian/moveexamples $(EXAMPLE_TEMP) $(DOC_DIR)
# Install man pages
	$(MAKE) -C install/man installman $(INSTALLOPTS) INSTALL_PREFIX=$(INSTALL_DIR)/usr/share
# Install RTL demos
	$(MAKE) -C demo sourceinstall $(INSTALLOPTS) INSTALL_SOURCEDIR=$(DOC_DIR)/fp-compiler
# Install whatsnew and readme
	$(MAKE) -C install/doc installdoc $(INSTALLOPTS) INSTALL_DOCDIR=$(DOC_DIR)/fp-compiler
# Create fpc.cfg which is included as conffile
	/bin/bash fpcsrc/compiler/utils/samplecfg \
		/usr/lib/fpc/$(FPCVERSION) $(INSTALL_DIR)/etc
	install -D debian/fpc-depends debian/tmp/usr/bin/
	set -x; for PACKAGE in fp-compiler fp-units-gfx fp-units-net fp-utils; do \
	  install -m644 -D debian/overrides/$$PACKAGE                             \
	    debian/$$PACKAGE/usr/share/lintian/overrides/$$PACKAGE;               \
	done
	touch install-arch-stamp

arrange-arch: arrange-arch-stamp
arrange-arch-stamp: install-arch-stamp debian-files-stamp
	dh_testdir
	dh_testroot
	dh_install -s --sourcedir=debian/tmp --list-missing

	touch arrange-arch-stamp

build-indep: patch
install-indep:
	touch install-indep-stamp

###################
# Documentation
#

build-doc: patch build-doc-stamp
build-doc-stamp: debian-files-stamp
	@echo "--- Building Documentation"
	dh_testdir
	$(MAKE) -C fpcdocs $(CONVERTER) $(BUILDOPTS) FPDOC=$(NEWFPDOC)
	touch build-indep-stamp

install-doc: install-doc-stamp
install-doc-stamp: build-doc-stamp debian-files-stamp
	@echo "--- Installing Documentation"
	dh_testdir
	dh_testroot
	dh_clean -k
	$(MAKE) -C fpcdocs $(DOCTYPE)install $(INSTALLOPTS) INSTALL_DOCDIR=$(INSTALL_DIR)/usr/share/doc/fp-docs
	touch install-doc-stamp

###################
# Source
#

install-source: install-source-stamp
install-source-stamp: 
	@echo "--- Cleaning the tree and copying the source code"
	dh_testdir
	dh_testroot
	$(MAKE) -C fpcsrc compiler_distclean
	$(MAKE) -C fpcsrc rtl_distclean
	$(MAKE) -C fpcsrc packages_distclean
	$(MAKE) -C fpcsrc ide_distclean
	$(MAKE) -C fpcsrc utils_distclean
	-${CLEAN_ALL}
	mkdir -p ${SRC_DIR}
	cp -Rfpl -t ${SRC_DIR} \
	$(CURDIR)/fpcsrc/compiler \
	$(CURDIR)/fpcsrc/packages \
	$(CURDIR)/fpcsrc/rtl
	touch install-source-stamp

###################
# Generic
#

build: build-arch build-indep
install: install-arch install-indep
binary: binary-arch binary-indep


###################
# Deb building
#

binary-indep: build-doc install-doc install-source debian-files
	@echo "--- Building: arch-indep packages"
	dh_testdir
	dh_testroot
	dh_installdocs -i
	dh_installchangelogs -i
	dh_install -i --sourcedir=debian/tmp --list-missing
	dh_compress -i -X.pdf
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i $(DEB_DH_BUILDDEB_ARGS)

binary-arch: build-arch arrange-arch debian-files
	@echo "--- Building: arch packages"
	dh_testdir
	dh_testroot
	dh_link
	dh_installdocs -a
	dh_installchangelogs -a
	dh_installexamples -a
	pod2man -c 'Free pascal for Debian GNU/Linux' \
	  debian/fpc-depends > debian/fpc-depends.1
	dh_installman -s
	dh_strip -s
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -s
	dh_shlibdeps -s
	dh_gencontrol -s
	dh_md5sums -s
	dh_builddeb -s $(DEB_DH_BUILDDEB_ARGS)

.PHONY: build clean binary binary-arch \
	binary-indep debian-files build-arch \
	install install-indep install-arch \
	configure

get-orig-source:
	-uscan --upstream-version=0 --rename
